<?php

namespace App\Http\Resources;

use App\Repositories\PollingRepo;
use App\Services\ImageServ;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;

class PollingResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $timeOutHour = floatval($this->timeout_hour);
        return [
            'id' => $this->id,
            'number' => $this->number,
            'type' => $this->type,
            'status' => $this->status,
            'status_text' => $this->status_text,
            'customer_id' => $this->customer_id,
            'customer_name' => $this->customer_name,
            'customer_address' => $this->customer_address,
            'customer_user_id' => $this->customer_user_id,
            'customer_user_name' => $this->customer_user_name,
            'customer_user_telephone' => $this->customer_user_telephone,
            'customer_sign' => $this->customer_sign ? true : false,
            'customer_sign_image' => (new ImageServ($this->customer_sign))->getUrl(),
            'plan_at' => $this->plan_at,
            'description' => $this->description,
            'user_id' => $this->user_id,
            'user_name' => $this->user_name,
            'create_user_id' => $this->create_user_id,
            'create_user_name' => $this->create_user_name,
            'acted' => boolval($this->acted),
            'exception' => boolval($this->exception),
            'complete_at' => $this->complete_at,
            'complete_description' => $this->complete_description,
            'timeout_hour' => $timeOutHour,
            'is_timeout' => $this->is_timeout,
            'btn_delete' => $this->btn_delete,
            'btn_accept' => $this->btn_accept,
            'btn_start' => $this->btn_start,
            'btn_complete' => $this->btn_complete,
            'images' => $this->getImages(),
            'histories' => $this->getHistories(),
            'create_time' => Carbon::parse($this->create_time)->toDateTimeString(),
            'update_time' => Carbon::parse($this->update_time)->toDateTimeString(),
        ];
    }

    private function getHistories()
    {
        $histories = $this->histories->toArray();
        if (empty($histories)) {
            return [];
        }

        $columns = array_column($histories, 'create_time');
        array_multisort($columns, SORT_ASC, $histories);
        $result = [];


        foreach ($histories as $history) {
            $content = $history['content'];
            $result[] = [
                'user' => $history['actor_name'],
                'status' => $history['status'],
                'status_text' => PollingRepo::getStatusText($history['status']),
                'content' => $content,
                'create_time' => Carbon::parse($history['create_time'])->toDateTimeString(),
            ];
        }

        return $result;
    }

    private function getImages()
    {
        $images = $this->images;
        if (empty($images)) {
            return [];
        }
        $urls = [];
        foreach ($images as $image) {
            $urls[] = (new ImageServ($image->image))->getUrl();
        }
        return $urls;
    }
}
